#!/usr/bin/python
# -*- coding: utf-8 -*-



import os
import sys
import re
from PyQt4 import QtCore, QtGui
from Main import *
from gestion import *
from DAC.CampagneDAC import Campagne

class Page5(object):
    def __init__(self,session):
		self.session=session
		self.app = QtGui.QApplication(sys.argv)
		self.window=QtGui.QDialog()
                self.parametre={}
                self.config={}
                self.step=50
                self.retour=4
                self.next=7
                self.env=0
                self.radio=1
                self.stattrafic=0
                self.manuel=0
                self.automatique=0
		self.parametre ={}


    def recup_dico(self,filename):
        fichier=open(filename,'r')
        expr=re.compile("[A-Za-z]")
        for line in fichier.readlines():
           if expr.findall(line):
               self.parametre[line.split(":")[0].split("\n")[0]]=line.split(":")[1].split("\n")[0]
        fichier.close()

    def suivant(self):
       
       self.recup_dico("dicosalle")
       idsalle = self.parametre["id"]
       self.recup_dico("dicoaccesspoint")
       idAP = self.parametre["id"]
       self.dicoCampagne["nom"] =  self.lineEdit_nom_utilisateur_vide.displayText()
       self.dicoCampagne["statradio"] =  self.checkBox_radio.isChecked()
       self.dicoCampagne["stattrafic"] =  self.checkBox_trafic.isChecked()
       self.dicoCampagne["env"] =   self.checkBox_environnement.isChecked()
       self.dicoCampagne["automatique"] =  self.radioButton_automatique.isChecked()
       self.dicoCampagne["manuel"] =  self.radioButton_manuelle.isChecked()
       self.dicoCampagne["step"] =  self.doubleSpinBox_step.value()
       
       camp = Campagne()
       camp.createByDico(self.dicoCampagne)
       camp.insertToDb(idsalle,idAP)       

       file=open("dicocampagne","a") 
       file.write("\nnom:"+self.lineEdit_nom_utilisateur_vide.displayText())
       file.write("\nstep:"+str(self.step))
       file.write("\nstatradio:"+str(self.radio))
       file.write("\nstattrafic:"+str(self.stattrafic))
       file.write("\nenv:"+str(self.env))
       file.write("\nautomatique:"+str(self.automatique))
       file.write("\nmanuel:"+str(self.manuel))
       file.write("\nid:"+str(camp.id["value"]))
       file.close()


       self.window.close()
       
       obj6=Main()
       if self.next == 6 :
         obj6.func6(self.config)
       if self.next == 7 :
         obj6.func7(self.config)
   
      #gestion.iwc(dic)    
    def iwlist(self):
       coordonee={}
       self.env=1
       #coordonee["x"]=0
       #coordonee["y"]=0
       #gestion1=Gestion("un")
       #gestion1.inserbddiwli(coordonee)  
    #   file=open("dicocampagne","a")
     #  file.write("\nenv:1")
      # file.close()


       
    def affich(self,param):
        #self.parametre=param
	self.setupUi(self.window)
	self.window.show()
        sys.exit(self.app.exec_())



    def pas(self):
       
   #    file=open("dicocampagne","a")
    #   file.write("\nstep:"+str(int(self.doubleSpinBox_step.value())))
     #  file.close()
        self.step=int(self.doubleSpinBox_step.value())
    def traffic(self):
        
       self.next = 6
   #    file=open("dicocampagne","a")
   #    file.write("\nstattrafic:1")
   #    file.close()
       self.stattrafic=1

    def rad(self):
       self.radio=1
     #  file=open("dicocampagne","a")
      # file.write("\nradio:1")
       #file.close()
    def manu(self):
       self.manuel=1
    def auto(self):
        self.automatique=1
    def precedent(self):
        
        self.window.close()
        commande="./Main.py "+str(self.retour)
        os.system(commande)

    def setupUi(self, Dialog):
        os.system("echo>dicocampagne")
    
 	self.dicoCampagne = {}
        Dialog.setObjectName("Dialog")
        Dialog.resize(1270, 721)
        self.lineEdit_campagne_mesures = QtGui.QLineEdit(Dialog)
        self.lineEdit_campagne_mesures.setGeometry(QtCore.QRect(240, 20, 801, 66))
        font = QtGui.QFont()
        font.setFamily("Bitstream Charter")
        font.setPointSize(32)
        font.setWeight(50)
        font.setBold(False)
        self.lineEdit_campagne_mesures.setFont(font)
        self.lineEdit_campagne_mesures.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit_campagne_mesures.setReadOnly(True)
        self.lineEdit_campagne_mesures.setObjectName("lineEdit_campagne_mesures")
        self.lineEdit_nom_utilisateur = QtGui.QLineEdit(Dialog)
        self.lineEdit_nom_utilisateur.setGeometry(QtCore.QRect(20, 190, 281, 41))
        font = QtGui.QFont()
        font.setFamily("Bitstream Charter")
        font.setPointSize(14)
        self.lineEdit_nom_utilisateur.setFont(font)
        self.lineEdit_nom_utilisateur.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit_nom_utilisateur.setReadOnly(True)
        self.lineEdit_nom_utilisateur.setObjectName("lineEdit_nom_utilisateur")
        self.lineEdit_nom_utilisateur_vide = QtGui.QLineEdit(Dialog)
        self.lineEdit_nom_utilisateur_vide.setGeometry(QtCore.QRect(310, 190, 911, 41))
        font = QtGui.QFont()
        font.setFamily("Bitstream Charter")
        font.setPointSize(14)
        self.lineEdit_nom_utilisateur_vide.setFont(font)
        self.lineEdit_nom_utilisateur_vide.setObjectName("lineEdit_nom_utilisateur_vide")
        self.lineEdit_mesures = QtGui.QLineEdit(Dialog)
        self.lineEdit_mesures.setGeometry(QtCore.QRect(30, 290, 271, 41))
        font = QtGui.QFont()
        font.setFamily("Bitstream Charter")
        font.setPointSize(14)
        font.setItalic(False)
        self.lineEdit_mesures.setFont(font)
        self.lineEdit_mesures.setAutoFillBackground(False)
        self.lineEdit_mesures.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit_mesures.setReadOnly(True)
        self.lineEdit_mesures.setObjectName("lineEdit_mesures")
        self.pushButton_suivant = QtGui.QPushButton(Dialog)
        self.pushButton_suivant.setGeometry(QtCore.QRect(1064, 650, 141, 41))
        font = QtGui.QFont()
        font.setFamily("Bitstream Charter")
        font.setPointSize(12)
        self.pushButton_suivant.setFont(font)
        self.pushButton_suivant.setObjectName("pushButton_suivant")
        self.pushButton_precedent = QtGui.QPushButton(Dialog)
        self.pushButton_precedent.setGeometry(QtCore.QRect(890, 650, 171, 41))
        font = QtGui.QFont()
        font.setFamily("Bitstream Charter")
        font.setPointSize(12)
        self.pushButton_precedent.setFont(font)
        self.pushButton_precedent.setObjectName("pushButton_precedent")
        self.lineEdit_deplacement = QtGui.QLineEdit(Dialog)
        self.lineEdit_deplacement.setGeometry(QtCore.QRect(680, 290, 371, 41))
        font = QtGui.QFont()
        font.setFamily("Bitstream Charter")
        font.setPointSize(14)
        self.lineEdit_deplacement.setFont(font)
        self.lineEdit_deplacement.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit_deplacement.setReadOnly(True)
        self.lineEdit_deplacement.setObjectName("lineEdit_deplacement")
        self.radioButton_manuelle = QtGui.QRadioButton(Dialog)
        self.radioButton_manuelle.setGeometry(QtCore.QRect(710, 370, 191, 25))
        font = QtGui.QFont()
        font.setFamily("Bitstream Charter")
        font.setPointSize(14)
        self.radioButton_manuelle.setFont(font)
        self.radioButton_manuelle.setObjectName("radioButton_manuelle")
        self.radioButton_automatique = QtGui.QRadioButton(Dialog)
        self.radioButton_automatique.setGeometry(QtCore.QRect(710, 420, 201, 25))
        font = QtGui.QFont()
        font.setFamily("Bitstream Charter")
        font.setPointSize(14)
        self.radioButton_automatique.setFont(font)
        self.radioButton_automatique.setObjectName("radioButton_automatique")
        self.checkBox_environnement = QtGui.QCheckBox(Dialog)
        self.checkBox_environnement.setGeometry(QtCore.QRect(100, 360, 451, 41))
        font = QtGui.QFont()
        font.setFamily("Bitstream Charter")
        font.setPointSize(14)
        self.checkBox_environnement.setFont(font)
        self.checkBox_environnement.setObjectName("checkBox_environnement")
        self.checkBox_radio = QtGui.QCheckBox(Dialog)
        self.checkBox_radio.setGeometry(QtCore.QRect(100, 420, 211, 41))
        font = QtGui.QFont()
        font.setFamily("Bitstream Charter")
        font.setPointSize(14)
        self.checkBox_radio.setFont(font)
        self.checkBox_radio.setObjectName("checkBox_radio")
        self.checkBox_trafic = QtGui.QCheckBox(Dialog)
        self.checkBox_trafic.setGeometry(QtCore.QRect(100, 490, 191, 41))
        font = QtGui.QFont()
        font.setFamily("Bitstream Charter")
        font.setPointSize(14)
        self.checkBox_trafic.setFont(font)
        self.checkBox_trafic.setObjectName("checkBox_trafic")
        self.doubleSpinBox_step = QtGui.QDoubleSpinBox(Dialog)
        self.doubleSpinBox_step.setGeometry(QtCore.QRect(1000, 370, 67, 41))
        font = QtGui.QFont()
        font.setFamily("Bitstream Charter")
        font.setPointSize(14)
        self.doubleSpinBox_step.setFont(font)
        self.doubleSpinBox_step.setDecimals(0)
        self.doubleSpinBox_step.setMinimum(50)
        self.doubleSpinBox_step.setMaximum(500)
        self.doubleSpinBox_step.setSingleStep(50)
        self.doubleSpinBox_step.setObjectName("doubleSpinBox_step")
        self.doubleSpinBox_step.setVisible(True)
        self.lineEdit_step = QtGui.QLineEdit(Dialog)
        self.lineEdit_step.setGeometry(QtCore.QRect(910, 370, 71, 41))
        font = QtGui.QFont()
        font.setFamily("Bitstream Charter")
        font.setPointSize(14)
        self.lineEdit_step.setFont(font)
        self.lineEdit_step.setReadOnly(True)
        self.lineEdit_step.setObjectName("lineEdit_step")
        self.lineEdit_step.setVisible(True)

        self.retranslateUi(Dialog)
        
        QtCore.QObject.connect(self.checkBox_trafic, QtCore.SIGNAL("clicked()"),self.traffic)
        QtCore.QObject.connect(self.radioButton_automatique, QtCore.SIGNAL("clicked()"), self.auto)
        QtCore.QObject.connect(self.radioButton_manuelle, QtCore.SIGNAL("clicked()"), self.manu)
        QtCore.QObject.connect(self.checkBox_radio, QtCore.SIGNAL("clicked()"),self.rad)
        QtCore.QObject.connect(self.pushButton_precedent, QtCore.SIGNAL("clicked()"), self.precedent)
        QtCore.QObject.connect(self.pushButton_suivant, QtCore.SIGNAL("clicked()"), self.suivant)
        QtCore.QObject.connect(self.checkBox_environnement, QtCore.SIGNAL("clicked()"), self.iwlist)    
        QtCore.QObject.connect(self.doubleSpinBox_step, QtCore.SIGNAL("valueChanged(double)"), self.pas)     
        
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_campagne_mesures.setText(QtGui.QApplication.translate("Dialog", "CAMPAGNE DE MESURES", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_nom_utilisateur.setText(QtGui.QApplication.translate("Dialog", "Nom de l\'utilisateur :", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_mesures.setText(QtGui.QApplication.translate("Dialog", "Mesures à faire :", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_suivant.setText(QtGui.QApplication.translate("Dialog", "Suivant >>", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_precedent.setText(QtGui.QApplication.translate("Dialog", "<< Précédent", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_deplacement.setText(QtGui.QApplication.translate("Dialog", "Déplacement des mesures :", None, QtGui.QApplication.UnicodeUTF8))
        self.radioButton_manuelle.setText(QtGui.QApplication.translate("Dialog", "Manuelle", None, QtGui.QApplication.UnicodeUTF8))
        self.radioButton_automatique.setText(QtGui.QApplication.translate("Dialog", "Automatique", None, QtGui.QApplication.UnicodeUTF8))
        self.checkBox_environnement.setText(QtGui.QApplication.translate("Dialog", "Environnement perturbé", None, QtGui.QApplication.UnicodeUTF8))
        self.checkBox_radio.setText(QtGui.QApplication.translate("Dialog", "Etude radio", None, QtGui.QApplication.UnicodeUTF8))
        self.checkBox_trafic.setText(QtGui.QApplication.translate("Dialog", "Etude traffic", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_step.setText(QtGui.QApplication.translate("Dialog", "STEP :", None, QtGui.QApplication.UnicodeUTF8))

